package 实验六;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

import 实验六.Course;
import 实验六.Student;
import 实验六.Teacher;

public class Main {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        Teacher teacher1 = new Teacher("王老师", 001, "男");
        Teacher teacher2 = new Teacher("张老师", 002, "女");

        // 王老师添加 课程1和课程2
        Course course1 = teacher1.addCourse(1, "C语言", "A101", "8:00");
        Course course2 = teacher1.addCourse(2, "JAVA", "A102", "10:00");

        // 张老师添加 课程3
        Course course3 = teacher2.addCourse(3, "HTML", "A101", "14:00");

        // 实例化 学生1和学生2
        Student student1 = new Student("小李", 20201001, "男");
        Student student2 = new Student("小华", 20201002, "女");

        // 学生1选课 课程1和课程2
        student1.selectCourse(course1);
        student1.selectCourse(course2);
        student2.selectCourse(course3);
        List<Course> courseList1 = student1.getCourseList();

        // File f = new File("/Users/lee/Desktop/未命名.txt");
        // windows系统
        File f = new File("/Users/lee/Desktop/未命名.txt ");
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
        out.writeObject(courseList1);
        out.close();

        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
            List<Course> array = (List<Course>) in.readObject();
            for (int i = 0; i < array.size(); i++) {
                Course course = array.get(i);
                System.out.println("课程" + (i + 1) + course.toString());
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }


        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
            List<Course> array = (List<Course>) in.readObject();
            student1.unSelectCourse(course1);
            System.out.println("学生1【退课】【课程1】后课程信息:");
            for (int i = 0; i < courseList1.size(); i++) {
                Course course = courseList1.get(i);
                System.out.printf("课程" + (i + 1) + course.toString());
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }
}}